﻿using System;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TelecoSystems.Infrastructure.Helpers;
using TelecoSystems.Web;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.ServiceModel.DomainServices.Client;
using TelecoSystems.Infrastructure.Views;
using System.ComponentModel.DataAnnotations;

namespace TelecoSystems.ViewModels.Profile {
    public class UserProfileViewModel : ViewModelBase {
        #region Fields
        private AuthenticationContext _authCtx; 
        #endregion

        #region Constructors
        public UserProfileViewModel() {
            _authCtx = new AuthenticationContext();

            _authCtx.Load<User>(_authCtx.GetUserQuery(), UserLoaded, null);

            UserDetail = WebContext.Current.User;
            PasswordChange = new PasswordChange();

            SaveUserCommand = new DelegateCommand<object>((o) => {
                _authCtx.SubmitChanges(SubmitChangesCompleted, null);
            }, (o) => true);
            ChangePasswordCommand = new DelegateCommand<object>((o) => {
                UserDetail.ChangePassword(PasswordChange.OldPassword, PasswordChange.NewPassword);
                SaveUserCommand.Execute(null);
            }, (o) => {
                return (PasswordChange.NewPassword == PasswordChange.NewPasswordReply) && (PasswordChange.OldPassword != "");
            });
        }        
        #endregion

        #region VM Properties
        private User _userDetail;
        public User UserDetail {
            get {
                return _userDetail;
            }
            set {
                _userDetail = value;
                NotifyPropertyChanged("UserDetail");
            }
        }

        private PasswordChange _passwordChange;
        public PasswordChange PasswordChange {
            get {
                return _passwordChange;
            }
            set {
                if (_passwordChange != value) {
                    _passwordChange = value;
                    NotifyPropertyChanged("PasswordChange");
                }
            }
        }
        #endregion

        #region Commands
        public DelegateCommand<object> SaveUserCommand { get; set; }
        public DelegateCommand<object> ChangePasswordCommand { get; set; }
        #endregion

        #region Event Handlers
        protected void UserLoaded(LoadOperation<User> loadOperation) {
            if (loadOperation.HasError) {
                ErrorWindow.CreateNew(loadOperation.Error);
            } else {
                UserDetail = loadOperation.Entities.FirstOrDefault();
            }
        }

        protected void SubmitChangesCompleted(SubmitOperation submit) {
            if (submit.HasError) {
                ErrorWindow.CreateNew(submit.Error);
            } else {
                WebContext.Current.User.FriendlyName = UserDetail.FriendlyName;
                InformationWindow.Create("Changes were saved successfully.", "Information");
            }
        } 
        #endregion
    }

    public class PasswordChange{        
        public string OldPassword { get; set; }        
        public string NewPassword { get; set; }
        [StringLength(16)]
        [Required]        
        public string NewPasswordReply { get; set; }
    }
}
